package by.grsu.kononovich.page.simple;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.kononovich.app.MyFirstWebApplication;
import by.grsu.kononovich.dao.impl.DamageInfoDao;
import by.grsu.kononovich.entity.DamageInfo;

public class DamageInfoPage extends WebPage {
	public DamageInfoPage() {
		super();

		
		final DamageInfoDao damageInfoDao = new DamageInfoDao(MyFirstWebApplication.DATA_XML_FOLDER);

		
		final List<DamageInfo> allDmgInf = damageInfoDao.getAll();

		
		add(new DataView<DamageInfo>("table-rows", new ListDataProvider<DamageInfo>(allDmgInf)) {
			@Override
			protected void populateItem(final Item<DamageInfo> item) {
				final DamageInfo dmgInf = item.getModelObject();
				
				item.add(new Label("id", dmgInf.getId()));
				item.add(new Label("Damage", dmgInf.getDamage()));
				item.add(new Label("Damagecost", dmgInf.getDamagecost()));
				item.add(new Label("Refusal", dmgInf.getRefusal()));
				item.add(new Label("Reason", dmgInf.getReason()));

				
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						
						setResponsePage(new DamageInfoEditPage(dmgInf));
						System.out.println("Damage Info "+dmgInf.getDamage()+" "+dmgInf.getDamagecost()+" "+dmgInf.getRefusal()+" "+dmgInf.getReason()+" edit");
					}
				});

				
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
					
						 damageInfoDao.delete(dmgInf.getId());
						
						setResponsePage(new DamageInfoPage());
						System.out.println("Damage Info "+dmgInf.getDamage()+" "+dmgInf.getDamagecost()+" "+dmgInf.getRefusal()+" "+dmgInf.getReason()+" deleted");
					}
				});
			}
		});

		
		add(new Link("createLink") {
			@Override
			public void onClick() {
				
				setResponsePage(new DamageInfoEditPage(new DamageInfo()));
				System.out.println("button CREAT NEW DAMAGE INFO pressed");
			}
		});
		}

}
